<template>
  <div class="org-wrap">
    <el-dialog :visible.sync="dialogVisible_org.show" center>
      <div class="show-title">设备序列号：{{ sn }}</div>
      <div class="show-title">设备名称：{{ name }}</div>

      <!-- 3 -->
      <el-form
        :inline="true"
        :model="form3_ruleform"
        ref="form3"
        :rules="form3_rules"
        class="demo-form-inline form3"
      >
        <el-form-item label="选择分组" prop="selOrg">
          <el-checkbox-group v-model="form3_ruleform.selOrg">
            <el-checkbox
              :label="item.org_id"
              :key="item.org_id"
              :org_id="item.org_id"
              v-for="item in orgLabel"
              >{{ item.gfather_name+" - "+item.father_name+" - "+item.org_name }}</el-checkbox
            >
          </el-checkbox-group>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" plain @click="onSubmit3('form3')"
            >保存</el-button
          >
          <el-button type="warning" plain @click="onReset('form3')"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { getData, postData } from "@/request/http";
export default {
  name: "adminSetting",
  components: {},
  props: {
    sn: {
      type: String,
      default: "",
    },
    name: {
      type: String,
      default: "",
    },
    dialogVisible_org: {
      type: Object,
      default() {
        return {
          show: false,
        };
      },
    },
  },
  data() {
    return {
      sn_seld: "",
      orgLabel: [], //添加中，组织机构的label
      // form3: {
      form3_ruleform: {
        selOrg: [],
      },
      form3_rules: {
        selOrg: [
          {
            type: "array",
            required: false,
            message: "请至少选择一个分组",
            trigger: "change",
          },
        ],
      },
    };
  },
  watch: {
    sn: {
      handler(newVal) {
        this.sn_seld = newVal;
        //this.$set(this.form1ruleform, "user_wanted", newVal);
        this.getOrgLabel();
      },
      immediate: true,
    },
  },
  computed: {},
  created() {},
  mounted() {},
  methods: {
    //添加提交333
    onSubmit3(formName) {
      var that = this;
      that.$refs[formName].validate((valid) => {
        //console.log(that.roleLabel);
        if (valid) {
          postData("/adminweb/device/org/set", {
            sn: that.sn_seld,
            orgs: that.form3_ruleform.selOrg,
          })
            .then((res) => {
              if (res.code == 0) {
                that.$message.success("修改成功！");
                that.getOrgLabel();
                that.$emit("refreshTabeList");
              } else if (res.code == -56055) {
                return;
              } else {
                that.$message.error(res.code + "，" + res.msg);
              }
            })
            .catch((res) => {
              that.$message.error(res);
            });
        } else {
           that.$message.error("修改失败");
          return false;
        }
      });
    },

    // 重置
    onReset(val) {
      this.$refs[val].resetFields();
    },

    //3获取管理员的组织机构的label
    getOrgLabel() {
      postData("/adminweb/device/org/list", {
        sn: this.sn_seld,
      })
        .then((res) => {
         // console.log(res);
          var arr = [];
          if (res.code == 0) {
            let orgs = res.datas.orgs;
            this.orgLabel = orgs;
            //初始的时候，根据authorized，勾选上
            for (var i = 0; i < orgs.length; i++) {
              if (orgs[i].authorized == 1) {
                arr.push(orgs[i].org_id);
              }
            }
            this.$set(this.form3_ruleform, "selOrg", arr);
          }else if (res.code == -56055) {
            return;
          } else {
            this.$message.error(res.code + "，" + res.msg);
          }
        })
        .catch((res) => {
          this.$message.error(res.code + "，" + res.msg);
        });
    },
  },
};
</script>
<style scoped>
/* 更改table,表头背景 */
.org-wrap >>> .el-table--border th,
.el-table__fixed-right-patch {
  /* background: #c0dee2; */
  background: #f0f3f4;
}
.org-wrap {
  width: 100%;
  height: auto;
  border-radius: 4px;
  border: 1px solid #ebeef5;
  background-color: #fff;
  overflow: hidden;
  color: #303133;
  -webkit-transition: 0.3s;
  transition: 0.3s;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}
.show-title {
  padding-left: 30px;
  margin-bottom: 10px;
  font-weight: 600;
}
.org-wrap >>> .el-form-item {
  width: 100% !important;
}

/* .org-wrap >>> .el-form-item:last-child {
  margin-left: 10% !important;
} */

.org-wrap >>> .demo-form-inline {
  width: 93% !important;
  height: 454px;
  min-height: 454px;
  box-shadow: rgba(0, 0, 0, 0.15) 0px 2px 5px;
  /* background: #fafafa; */
  border-radius: 5px;
  padding: 15px;
  overflow-x: hidden;
  overflow-y: scroll;
  margin-top: 0 !important;
}

.org-wrap >>> .el-dialog {
  width: 40%;
  height: 600px;
  
}
.org-wrap >>> .el-dialog__header {
  padding: 10px 0;
}
.org-wrap >>> .el-dialog__body {
  /* display: flex;
  justify-content: space-around;
  align-items: flex-start; */
  padding: 0;
}
/* .org-wrap >>> .demo-form-inline {
  margin-top: 0 !important;
} */
.org-wrap .form3 >>> .el-form-item {
  width: 100% !important;
  display: flex;
  flex-flow: column;
  justify-content: flex-start;
  align-items: flex-start;
}

/* .org-wrap .form3 >>> .el-form-item__label {
  display: block;
  float: left !important;
} */
.org-wrap .form3 >>> .el-checkbox {
  display: block !important;
}
</style>
